package com.wangcang.widgetlibrary.activity;

import android.os.Bundle;
import android.support.annotation.DrawableRes;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.wangcang.widgetlibrary.R;
import com.wangcang.widgetlibrary.adapter.MyViewAdapter;

import java.util.ArrayList;
import java.util.List;

public class TabLayoutActivity extends AppCompatActivity {

	private TabLayout mTableLayout;
	private ViewPager mViewPager;
	private List<View> mViewList = new ArrayList<>();
	private MyViewAdapter mAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_tab_layout);
		mTableLayout = (TabLayout) findViewById(R.id.tabs);
		mViewPager = (ViewPager) findViewById(R.id.view_pager);
		mViewList.add(getImageView(R.drawable.a1));
		mViewList.add(getImageView(R.drawable.a2));
		mViewList.add(getImageView(R.drawable.a3));
		mViewList.add(getImageView(R.drawable.a4));
		mAdapter = new MyViewAdapter(mViewList);
		mViewPager.setAdapter(mAdapter);
		mTableLayout.setupWithViewPager(mViewPager);
	}

	private ImageView getImageView(@DrawableRes int resId) {
		ImageView imageView = new ImageView(this);
		imageView.setImageResource(resId);
		imageView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
		return imageView;
	}
}
